package me.idashu.attg.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import me.idashu.attg.domain.AuditeEntity;
import me.idashu.attg.domain.CommentEntity;
import me.idashu.attg.domain.ContributionEntity;
import me.idashu.attg.domain.PageEntity;
import me.idashu.attg.domain.RecommendEntity;
import me.idashu.attg.domain.RecommendsDataStatisticsEntity;
import me.idashu.attg.service.RecommendService;
import me.idashu.attg.util.SystemConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
@RequestMapping("/recommends")
public class RecommendController {
	
	private SystemConfig systemConfig;
	
	private RecommendService recommendService;
	@Autowired
	public void setRecommendService(RecommendService recommendService) {
		this.recommendService = recommendService;
	}

	@RequestMapping("/recommendsList")
	public String recommendsList(Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		int total = recommendService.recommendCount();
		PageEntity pageEntity = new PageEntity();
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
		map.put("pageEntity", pageEntity);
		List<RecommendEntity> listRecommend = recommendService.getRecommendsList(map);
		if(listRecommend.size()<=0){
			listRecommend = null;
		}
		model.addAttribute("recommendList", "推荐内容列表");
    	model.addAttribute("listRecommend", listRecommend);
    	model.addAttribute("pageEntity", pageEntity);
		return "recommend_list";
	}
	
	@RequestMapping("/deleteRecommends")
	public String deleteRecommends(@RequestParam("recommendId") int recommendId,Model model){
		recommendService.deleteRecommends(recommendId);
		recommendsList(model);
		return "recommend_list";
	}
	
	
	@RequestMapping("/recommendsAjaxList")
	public String recommendsAjaxList(Model model,@ModelAttribute PageEntity pageEntity){
		Map<String, Object> map = new HashMap<String, Object>();
		int total = recommendService.recommendCount();
    	pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
    	map.put("pageEntity", pageEntity);
    	List<RecommendEntity> listRecommend = recommendService.getRecommendsList(map);
    	model.addAttribute("recommendList", "推荐内容列表");
    	model.addAttribute("listRecommend", listRecommend);
    	model.addAttribute("pageEntity", pageEntity);
		return "recommend_list";
	}
	
	@RequestMapping("/auditeList")
	public String auditeList(Model model,@RequestParam("recommendId") int recommendId){
		Map<String, Object> map = new HashMap<String, Object>();
		int total = recommendService.auditeCount(recommendId);
		PageEntity pageEntity = new PageEntity();
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
		map.put("pageEntity", pageEntity);
		map.put("recommendId", recommendId);
		List<AuditeEntity> listAudite = recommendService.auditeList(map);
		if(listAudite.size()<=0){
			listAudite = null;
		}
    	model.addAttribute("listAudite", listAudite);
    	model.addAttribute("pageEntity", pageEntity);
    	return "auditeAjaxList";
	}
	
	@RequestMapping("/auditeAjaxList")
	public String auditeAjaxList(Model model,@ModelAttribute PageEntity pageEntity,@RequestParam("recommendId") int recommendId){
		Map<String, Object> map = new HashMap<String, Object>();
		int total = recommendService.auditeCount(recommendId);
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
		map.put("pageEntity", pageEntity);
		map.put("recommendId", recommendId);
		List<AuditeEntity> listAudite = recommendService.auditeList(map);
		if(listAudite.size()<=0){
			listAudite = null;
		}
    	model.addAttribute("listAudite", listAudite);
    	model.addAttribute("pageEntity", pageEntity);
    	return "auditeAjaxList";
	}
	
	@RequestMapping("/recommendsDataStatistics")
	public String recommendsDataStatistics(Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		int total = recommendService.recommendCount();
		PageEntity pageEntity = new PageEntity();
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
		map.put("pageEntity", pageEntity);
		List<RecommendsDataStatisticsEntity> list = recommendService.getRecommendsDataStatisticsList(map);
		if(list.size()<=0){
			list = null;
		}
		model.addAttribute("recommendsDataStatisticsList", "推荐内容数据统计列表");
    	model.addAttribute("list", list);
    	model.addAttribute("pageEntity", pageEntity);
		return "recommendsDataStatistics_list";
	}
	
	@RequestMapping("/recommendsDataStatisticsAjaxList")
	public String recommendsDataStatisticsAjaxList(Model model,@ModelAttribute PageEntity pageEntity){
		Map<String, Object> map = new HashMap<String, Object>();
		int total = recommendService.recommendCount();
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
		map.put("pageEntity", pageEntity);
		List<RecommendsDataStatisticsEntity> list = recommendService.getRecommendsDataStatisticsList(map);
		if(list.size()<=0){
			list = null;
		}
		model.addAttribute("recommendsDataStatisticsList", "推荐内容数据统计列表");
    	model.addAttribute("list", list);
    	model.addAttribute("pageEntity", pageEntity);
		return "recommendsDataStatistics_list";
	}
	
	@RequestMapping("/recommendsPartExport")
	public void recommendsPartExport(@RequestParam("dateFrom") String dateFrom,@RequestParam("dateTo") String dateTo,HttpServletResponse response){
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			dateFrom = dateFrom+" 00:00:00";
			dateTo = dateTo+" 23:59:59";
			map.put("dateFrom", dateFrom);
			map.put("dateTo", dateTo);
			List<RecommendsDataStatisticsEntity> list = recommendService.getRecommendsDataStatisticsList(map);
			String xlsPath = systemConfig.getDir("xlsPath");
//			String xlsPath = "E:/推荐内容数据统计.xls";
			toXLS(list, xlsPath);
			downloadFile(new File(xlsPath),"result.xls",response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	@RequestMapping("/recommendsExport")
	public void recommendsExport(HttpServletResponse response){
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("dateFrom", "");
			map.put("dateTo", "");
			List<RecommendsDataStatisticsEntity> list = recommendService.getRecommendsDataStatisticsList(map);
			String xlsPath = systemConfig.getDir("xlsPath");
//			String xlsPath = "E:/推荐内容数据统计.xls";
			toXLS(list, xlsPath);
			downloadFile(new File(xlsPath),"result.xls",response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void toXLS(Collection collection,String path) {   
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			WritableWorkbook book = Workbook.createWorkbook(new File(path));
			WritableSheet sheet = book.createSheet("导出数据", 0);
			Label labe1 = new Label(0, 0, "推荐内容名称");
			Label labe2 = new Label(1, 0, "创建日期");
			Label labe3 = new Label(2, 0, "高级用户审核数量");
			Label labe4 = new Label(3, 0, "得到的推荐总数");
//			Label labe5 = new Label(4, 0, "注册用户推荐");
//			Label labe6 = new Label(5, 0, "游客推荐");
			
			sheet.addCell(labe1);
			sheet.addCell(labe2);
			sheet.addCell(labe3);
			sheet.addCell(labe4);
//			sheet.addCell(labe5);
//			sheet.addCell(labe6);
			
			Iterator it = collection.iterator();
			int i = 1;
			while(it.hasNext()) {
				RecommendsDataStatisticsEntity srrf = (RecommendsDataStatisticsEntity) it.next();
				Label label1 = new Label(0, i, srrf.getContent());
				Label label2 = new Label(1, i, sdf.format(srrf.getTime()));
				Label label3 = new Label(2, i, String.valueOf(srrf.getAuditeCount()));
				Label label4 = new Label(3, i, String.valueOf(srrf.getRescore()));
//				Label label5 = new Label(4, i, String.valueOf(srrf.getAuditeCount()));
//				Label label6 = new Label(5, i, String.valueOf(srrf.getAuditeCount()));
				
				sheet.addCell(label1);
				sheet.addCell(label2);
				sheet.addCell(label3);
				sheet.addCell(label4);
//				sheet.addCell(label5);
//				sheet.addCell(label6);
				i++;
			}
			book.write();
			book.close();
		} catch (Exception e) {    
			
			e.printStackTrace();    
		}
	}
	
	protected void downloadFile(File file,String fileName,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/oct-stream;charset=UTF-8");
		response.setHeader("Content-disposition",
				"attachment;filename="+fileName);
		BufferedInputStream in = new BufferedInputStream(new
				FileInputStream(file));
		BufferedOutputStream out = new BufferedOutputStream(response.
				getOutputStream());
		byte[] buff = new byte[4096];
		int length = -1;
		while ((length = in.read(buff)) != -1) {
			out.write(buff, 0, length);
		}
		in.close();
		out.close();
	}
}
